import com.system.UserPasswordEncoderListener
import grails.plugin.springsecurity.SpringSecurityUtils
import grails3.security.druid.DruidConnectionProvider

// Place your Spring DSL code here
beans = {

//    "stat-filter"(com.alibaba.druid.filter.stat.StatFilter) {
//        slowSqlMillis = 1
//        dbType = "mysql"
//        mergeSql = true
//        logSlowSql = true
//    }
//
//    wallConfig(com.alibaba.druid.wall.WallConfig) { bean ->
//        bean.initMethod = "init"
//        dir = "META-INF/druid/wall/mysql"
//    }
//
//    "wall-filter"(com.alibaba.druid.wall.WallFilter) {
//        dbType = "mysql"
//        logViolation = true
//        config = ref("wallConfig")
//    }
//
//    "logFilter"(com.alibaba.druid.filter.logging.Slf4jLogFilter) {
//        dataSourceLogEnabled = true
//        statementExecutableSqlLogEnable = true
//    }


    // alibaba druid配置
//    dataSource(com.alibaba.druid.pool.DruidDataSource) { bean ->
//        bean.initMethod = 'init'
//        bean.destroyMethod = 'close'
//        driverClassName = grailsApplication.config.dataSource.driverClassName
//        url = grailsApplication.config.dataSource.url
//        username = grailsApplication.config.dataSource.username
//        password = grailsApplication.config.dataSource.password
//        initialSize = grailsApplication.config.dataSource.druid.initialSize
//        minIdle = grailsApplication.config.dataSource.druid.minIdle
//        maxActive = grailsApplication.config.dataSource.druid.maxActive
//        maxWait = grailsApplication.config.dataSource.druid.maxWait
//        timeBetweenEvictionRunsMillis = grailsApplication.config.dataSource.druid.timeBetweenEvictionRunsMillis
//        minEvictableIdleTimeMillis = grailsApplication.config.dataSource.druid.minEvictableIdleTimeMillis
//        validationQuery = grailsApplication.config.dataSource.druid.validationQuery
//        removeAbandoned = grailsApplication.config.dataSource.druid.removeAbandoned
//        testWhileIdle = grailsApplication.config.dataSource.druid.testWhileIdle
//        connectionProperties = grailsApplication.config.dataSource.druid.connectionProperties
//        testOnBorrow = grailsApplication.config.dataSource.druid.testOnBorrow
//        poolPreparedStatements = grailsApplication.config.dataSource.druid.poolPreparedStatements
//        testOnReturn = grailsApplication.config.dataSource.druid.testOnReturn
//        useGlobalDataSourceStat = grailsApplication.config.dataSource.druid.useGlobalDataSourceStat
//        maxPoolPreparedStatementPerConnectionSize = grailsApplication.config.dataSource.druid.maxPoolPreparedStatementPerConnectionSize
//        filters = grailsApplication.config.dataSource.druid.filters
//        Properties p = new Properties()
//        p.put("druid.stat.mergeSql", "true")
//        p.put("druid.stat.slowSqlMillis", "200")
//        connectProperties = p
////        proxyFilters = [ref("stat-filter"), ref("wall-filter"), ref("logFilter")]
////        proxyFilters = [ref("statFilter"), ref("logFilter")]
//    }

//    provider(DruidConnectionProvider) {
//        dataSource = ref("dataSource")
//    }

    // 监控管理页面配置（管理用户名、密码）
//    druidConsoleServlet(org.springframework.boot.web.servlet.ServletRegistrationBean) {
//        servlet = bean(com.alibaba.druid.support.http.StatViewServlet)
//        initParameters = [
//                "loginUsername": "root",
//                "loginPassword": "123456",
//                "allow"        : "192.168.1.105/24,127.0.0.1",
//                "deny"         : ""
//        ]
//        urlMappings = ["/druid/*"]
//    }
//
//    webStatFilter(org.springframework.boot.web.servlet.FilterRegistrationBean) {
//        filter = bean(com.alibaba.druid.support.http.WebStatFilter)
//        initParameters = [
//                "exclusions"         : "*.js,*.gif,*.jpg,*.png,*.svg,*.css,*.ico,/druid/*",
//                "sessionStatMaxCount": "2000",
//                "sessionStatEnable"  : "true",
//                "principalCookieName": "verifyCode",
//                "profileEnable"      : "true"
//        ]
//        urlPatterns = ["/*"]
//    }

    authenticationProcessingFilter(com.system.AuthFilter) {
        def conf = SpringSecurityUtils.securityConfig
        authenticationManager = ref("authenticationManager")
        sessionAuthenticationStrategy = ref('sessionAuthenticationStrategy')
        authenticationSuccessHandler = ref('authenticationSuccessHandler')
        authenticationFailureHandler = ref('authenticationFailureHandler')
        rememberMeServices = ref('rememberMeServices')
        authenticationDetailsSource = ref('authenticationDetailsSource')
        filterProcessesUrl = conf.apf.filterProcessesUrl
        usernameParameter = conf.apf.usernameParameter
        passwordParameter = conf.apf.passwordParameter
        continueChainBeforeSuccessfulAuthentication = conf.apf.continueChainBeforeSuccessfulAuthentication
        allowSessionCreation = conf.apf.allowSessionCreation
        postOnly = conf.apf.postOnly
    }
    userPasswordEncoderListener(UserPasswordEncoderListener)


    userDetailsService(com.system.CustomUserDetailsService) {
        grailsApplication = ref('grailsApplication')
    }

}
